//Create BuyerStampDutyView
function BuyerStampDutyView(vWidth) {
    var self = Titanium.UI.createView({
        backgroundColor: 'transparent',
    });
    var section1 = Ti.UI.createTableViewSection({
        height: 'auto',
        headerTitle: 'Buyers Stamp Duty'
    });
    var row1 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblPurchasePrice = Titanium.UI.createLabel({
        text: 'Purchase Price include GST',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var txtPurchasePrice = Ti.UI.createTextField({
        left: vWidth / 2,
        right: 10,
        hintText: '',
        textAlign: "right",
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        keyboardType: Titanium.UI.KEYBOARD_DECIMAL_PAD,
        width: vWidth / 2 - 10
    });
    txtPurchasePrice.addEventListener('change', function (e) {
        Calc();
    });
    txtPurchasePrice.addEventListener('return', function (e) {
        txtPurchasePrice.blur();
    });
    var row2 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblPurchaseEntity = Titanium.UI.createLabel({
        text: 'Purchase Entity',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var PKTextField = require('view/PickerTextField');
    var pkPurchaseEntyData = [];
    pkPurchaseEntyData[0] = {
        title: 'Personal',
        valueId: 0
    };
    pkPurchaseEntyData[1] = {
        title: 'Company',
        valueId: 1
    };
    // pkPurchaseEntyData[2] = {
    // title: 'Industrial',
    // valueId: 2
    // };
    var txtPurchaseEntity = new PKTextField(self, 1, pkPurchaseEntyData);
    txtPurchaseEntity.addEventListener('pickerTextFieldChange', function (e) {
        valuePurchaseEntity = e.valueId;
        Calc();
    });
    txtPurchaseEntity.left = vWidth / 2;
    txtPurchaseEntity.width = vWidth / 2 - 10;
    var row3 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblPropertyType = Titanium.UI.createLabel({
        text: 'Type of Property',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var pkPropertyTypeData = [];
    pkPropertyTypeData[0] = Ti.UI.createPickerRow({
        title: 'Residential',
        valueId: 0
    });
    pkPropertyTypeData[1] = Ti.UI.createPickerRow({
        title: 'Commercial',
        valueId: 1
    });
    var txtPropertyType = new PKTextField(self, 1, pkPropertyTypeData);
    txtPropertyType.addEventListener('pickerTextFieldChange', function (e) {
        valuePropertyType = e.valueId;
        Calc();
    });
    txtPropertyType.left = vWidth / 2;
    txtPropertyType.width = vWidth / 2 - 10;
    var row4 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblBuyerNationality = Titanium.UI.createLabel({
        text: 'Buyers Nationality',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var pkBuyernationlityData = [];
    pkBuyernationlityData[0] = Ti.UI.createPickerRow({
        title: 'Singaporean',
        valueId: 0
    });
    pkBuyernationlityData[1] = Ti.UI.createPickerRow({
        title: 'PR',
        valueId: 1
    });
    pkBuyernationlityData[2] = Ti.UI.createPickerRow({
        title: 'Foreigner',
        valueId: 2
    });
    var txtBuyerNationality = new PKTextField(self, 1, pkBuyernationlityData);
    txtBuyerNationality.addEventListener('pickerTextFieldChange', function (e) {
        valueBuyersNationality = e.valueId;
        Calc();
    });
    txtBuyerNationality.left = vWidth / 2;
    txtBuyerNationality.width = vWidth / 2 - 10;
    var row5 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblNumberOfProperties = Titanium.UI.createLabel({
        text: 'Number of Properties including current purchase',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var pkNumberPropertyData = [];
    pkNumberPropertyData[0] = Ti.UI.createPickerRow({
        title: '1',
        valueId: 0
    });
    pkNumberPropertyData[1] = Ti.UI.createPickerRow({
        title: '2',
        valueId: 1
    });
    pkNumberPropertyData[2] = Ti.UI.createPickerRow({
        title: '3 or more',
        valueId: 2
    });
    var txtNumberOfProperties = new PKTextField(self, 1, pkNumberPropertyData);
    txtNumberOfProperties.addEventListener('pickerTextFieldChange', function (e) {
        valueNumberProperties = e.valueId;
        Calc();
    });
    txtNumberOfProperties.left = vWidth / 2;
    txtNumberOfProperties.width = vWidth / 2 - 10;
    row1.add(lblPurchasePrice);
    row1.add(txtPurchasePrice);
    row2.add(lblPurchaseEntity);
    row2.add(txtPurchaseEntity);
    row3.add(lblPropertyType);
    row3.add(txtPropertyType);
    row4.add(lblBuyerNationality);
    row4.add(txtBuyerNationality);
    row5.add(lblNumberOfProperties);
    row5.add(txtNumberOfProperties);
    section1.add(row1);
    section1.add(row2);
    section1.add(row3);
    section1.add(row4);
    section1.add(row5);
    var section2 = Ti.UI.createTableViewSection({
        height: 'auto',
        headerTitle: 'Buyers Stamp Duty Result'
    });
    var row6 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblTitle1 = Titanium.UI.createLabel({
        text: 'Additional Buyers Stamp Duty',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var lblAdditionalDuty = Titanium.UI.createLabel({
        text: 'v1',
        font: {
            fontSize: 20
        },
        color: 'red',
        left: vWidth / 2,
        width: vWidth / 2 - 2 * 10,
        textAlign: 'right'
    });
    var row7 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblTitle2 = Titanium.UI.createLabel({
        text: 'Buyers Stamp Duty',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var lblBuyersDuty = Titanium.UI.createLabel({
        text: 'v2',
        font: {
            fontSize: 20
        },
        color: 'red',
        left: vWidth / 2,
        width: vWidth / 2 - 2 * 10,
        textAlign: 'right'
    });
    var row8 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblTitle3 = Titanium.UI.createLabel({
        text: 'Total Buyer\'s Stamp Duty',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var lblTotalBuyersDuty = Titanium.UI.createLabel({
        text: 'v3',
        font: {
            fontSize: 20
        },
        color: 'red',
        left: vWidth / 2,
        width: vWidth / 2 - 2 * 10,
        textAlign: 'right'
    });
    row6.add(lblTitle1);
    row6.add(lblAdditionalDuty);
    row7.add(lblTitle2);
    row7.add(lblBuyersDuty);
    row8.add(lblTitle3);
    row8.add(lblTotalBuyersDuty);
    section2.add(row6);
    section2.add(row7);
    section2.add(row8);
    var data = [section1, section2];
    var table = Ti.UI.createTableView({
        top: 0,
        data: data,
        backgroundColor: 'transparent',
        separatorColor: 'yellow'
    });
    self.add(table);

    function Calc() {
        try {
            var byPass = 0;
            var purchasePrice = parseInt(txtPurchasePrice.value);
            if (isNaN(purchasePrice) || purchasePrice === 0) {
                byPass = 1;
            } else {
                var criteria = 180000;
                var delta = 100;
                var _1st = 0;
                var _2nd = 0;
                var _balance = 0;
                var stampDutyValue = 0;
                var _addedValue = 0;
                if (purchasePrice < criteria) {
                    _1st = purchasePrice;
                } else {
                    _1st = criteria;
                    if ((purchasePrice - criteria) < criteria) {
                        _2nd = purchasePrice - criteria;
                    } else {
                        _2nd = criteria;
                        _balance = (purchasePrice - 2 * criteria);
                    };
                    stampDutyValue = (_1st / 100) * 1 + (_2nd / 100) * 2 + (_balance / 100) * 3;
                };
                if (valuePropertyType === 1) {
                    _addedValue = 0;
                } else {
                    switch (valueBuyersNationality) {
                        case 0:
                            { //Singaporean
                                switch (valueNumberProperties) {
                                    case 0:
                                        _addedValue = purchasePrice * 0 / 100;
                                        break;
                                    case 1:
                                        _addedValue = purchasePrice * 7 / 100;
                                        break;
                                    case 2:
                                        _addedValue = purchasePrice * 10 / 100;
                                        break;
                                }
                                break;
                            }
                        case 1:
                            { //PR
                                switch (valueNumberProperties) {
                                    case 0:
                                        _addedValue = purchasePrice * 5 / 100;
                                        break;
                                    case 1:
                                        _addedValue = purchasePrice * 10 / 100;
                                        break;
                                    case 2:
                                        _addedValue = purchasePrice * 10 / 100;
                                        break;
                                }
                                break;
                            }
                        case 2:
                            {
                                switch (valueNumberProperties) {
                                    case 0:
                                        _addedValue = purchasePrice * 15 / 100;
                                        break;
                                    case 1:
                                        _addedValue = purchasePrice * 15 / 100;
                                        break;
                                    case 2:
                                        _addedValue = purchasePrice * 15 / 100;
                                        break;
                                }
                                break;
                            }
                    }
                };
                lblBuyersDuty.text = stampDutyValue.toString();
                lblAdditionalDuty.text = _addedValue.toString();
                lblTotalBuyersDuty.text = (purchasePrice + stampDutyValue + _addedValue).toString();
            };
            if (byPass === 0) {} else {
                lblBuyersDuty.text = '';
                lblAdditionalDuty.text = '';
                lblTotalBuyersDuty.text = '';
            };
        } catch (e) {
            Ti.API.info(e);
        }
    }
    var valuePurchaseEntity = 0;
    var valuePropertyType = 0;
    var valueBuyersNationality = 0;
    var valueNumberProperties = 0;
    var picker = null;
    return self;
}
module.exports = BuyerStampDutyView;